<?php
function is_ext($field_name){
	$tmpname = @$_FILES [$field_name] ['tmp_name'];
	if(is_uploaded_file($tmpname)) {
		$tempfile = @fopen($tmpname, "rb");
		$bin = fread($tempfile, 2); //只读2字节
		fclose($tempfile);
		$strInfo = @unpack("C2chars", $bin);
		$typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
		$fileType = false;
		switch ($typeCode){ // 6677:bmp 255216:jpg 7173:gif 13780:png 7790:exe 8297:rar 8075:zip tar:109121 7z:55122 gz 31139 8273:webp
			case '255216':
				$fileType = true;
				break;
			case '7173':
				$fileType = true;
				break;
			case '13780':
				$fileType = true;
				break;
			case '8273':
				$fileType = true;
				break;
			default:
				$fileType = false;
		}
		return $fileType;
	}else{
		return false;
	}
}
function new_caijian($img){
	$CI = &get_instance();
	list($width, $height) = getimagesize(FCPATH.$img);
	$config['image_library'] = 'gd2';
	$config['source_image'] = FCPATH.$img;
	$config['maintain_ratio'] = TRUE;
	if($width >= $height)
	{
		$config['master_dim'] = 'height';
	}else{
		$config['master_dim'] = 'width';
	}
	$config['width'] = 1000;
	$config['height'] = 1000;
	$CI->load->library('image_lib', $config);
	$CI->image_lib->resize();

	$CI->image_lib->clear();
}
function caijian_image($img,$width='',$thumb='',$quality=100,$sz=0){
	if (!$width)return false;
	if (!$img)return false;
	if (!file_exists(FCPATH.$thumb)){
		mkdir(FCPATH.$thumb);
	}
	$CI = &get_instance();
	$wimg = getimagesize(FCPATH.$img);
	$config['image_library'] = 'gd2';
	$config['source_image'] = FCPATH.$img;
	$config['maintain_ratio'] = TRUE;
	$config['quality'] = $quality;
	$config['width'] = $width;
	if ($thumb){
		$config['new_image'] = FCPATH.$thumb;
	}
	if ($wimg[0] > $width){
		if ($sz){
			$CI->image_lib->initialize($config);
		}else {
			$CI->load->library('image_lib', $config);
		}
		$CI->image_lib->resize();
// 			if ( ! $this->image_lib->resize()){
// 			    file_put_contents(FCPATH.'img.txt', $width.':'.$this->image_lib->display_errors()."\r\n",FILE_APPEND);
// 			}
		if ( ! $CI->image_lib->resize()){
			echo $CI->image_lib->display_errors();
		}
	}else {
		$CI->load->library('image_lib', $config);
	}
	$CI->image_lib->clear();
}
function wx_img_url($img){
    return base_url() . $img['folder'] . $img['filename'];
}

function download_wx_img($user){
    $img = FCPATH.'upload/qr_code/'.$user['id'].'.png';
    $img_url = str_replace('/0', '/132', $user['headimgurl']);
    file_put_contents($img, file_get_contents($img_url));
    return $img;
//    $img = new_caijian_6($img);
}

function student_book_img($article_id, $limit='', $is_no_cache = 0){
    $img = db_get_list('wx_img', array('article_id' => $article_id, 'tid' => 6, 'is_show' => 1, 'state' => 1), $limit);
    return $img;
}
function park_img($article_id, $limit='', $is_no_cache = 0){
    $img = db_get_list('wx_img', array('article_id' => $article_id, 'tid' => 6, 'is_show' => 1, 'state' => 1), $limit);
    return $img;
}
//随机获取一张信息图片做封面图
function park_cover_img($id){
    $r = park_img($id);
    if($r){
        $k = array_rand($r);
        return base_url() . $r[$k]['folder'] . $r[$k]['filename'];
    } else {
        return '';
    }
}
function park_1_img($id){
    $r = park_img($id, 1);
    if ($r) {
        return base_url() . $r[0]['folder'] . $r[0]['filename'];
    }
}
